package algorithm;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class TreeNodeTestt2 {
    public static void main(String[] args) {
        TreeNode root = TreeUtil.buildSearchTree();
        System.out.println(minDeepth(root));
    }

    private static void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.print(node.data);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }

    private static void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                // 栈顶元素
                TreeNode node = stack.pop();
                System.out.println(node.data);
                cur = node.right;
            }
        }
    }

    private static void postOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> res = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            // 中右左
            TreeNode node = stack.pop();
            res.push(node);
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        while (!res.isEmpty()) {
            TreeNode node = res.pop();
            System.out.print(node.data);
        }
    }

    private static void widthOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.println(node.data);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }

    public static int getTreeHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return 1 + Math.max(leftHeight, rightHeight);
    }

    // 二叉搜索树第k小的元素
    private static int findK(TreeNode root, int k) {
        if (root == null) {
            return -1;
        }
        int i = 1;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                TreeNode node = stack.pop();
                if (i++ == k) {
                    return node.data;
                }
                cur = node.right;
            }
        }
        return -1;
    }

    private static boolean isBalanceTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return Math.abs(leftHeight - rightHeight) <= 1;
    }

    public static boolean isComplete(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                queue.add(node.left);
                queue.add(node.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                return false;
            }
        }
        return true;
    }

    private static boolean isFull(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int num = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            if (size < num) {
                return false;
            }
            num *= 2;
        }
        return true;
    }

    /**
     * 二叉树节点的最大路径
     * 5
     * 3      7
     * 1    4
     * 2    6
     */
    public static int deepLength(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        // 左子树和右子树高度和
        int height = leftHeight + rightHeight + 1;
        // 左子树的最长路径
        int leftDeepHeight = deepLength(root.left);
        // 右子树的最长路径
        int rightDeepHeight = deepLength(root.right);
        return Math.max(Math.max(leftDeepHeight, rightDeepHeight), height);
    }

    private static boolean isSymmetrical(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetrical(root.left, root.right);
    }

    private static boolean isSymmetrical(TreeNode left, TreeNode right) {
        // 叶子节点，对称的
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            // 左子树和右子树有一个为空，不对称
            return false;
        }
        if (left.data != right.data) {
            // 值不相同，不对称
            return false;
        }
        return isSymmetrical(left.left, right.right) && isSymmetrical(left.right, right.left);
    }

    private static int minDeepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right != null) {
            return minDeepth(root.right) + 1;
        } else if (root.left != null && root.right == null) {
            return minDeepth(root.left) + 1;
        }
        return Math.min(minDeepth(root.left), minDeepth(root.right)) + 1;
    }
}
